﻿
namespace Aeon.Emulator.Instructions
{
    internal static class Cmp
    {
        [Opcode("38/r rmb,rb|3A/r rb,rmb|3C al,ib|80/7 rmb,ib|82/7 rmb,ib", AddressSize = 16 | 32, OperandSize = 16 | 32)]
        public static void ByteCompare(Processor p, byte value1, byte value2)
        {
            int sResult = (sbyte)value1 - (sbyte)value2;
            uint uResult = (uint)value1 - (uint)value2;

            p.ByteUpdateFlags(sResult, uResult, EFlags.Carry | EFlags.Overflow | EFlags.Sign | EFlags.Zero | EFlags.Parity);
        }

        [Opcode("39/r rmw,rw|3B/r rw,rmw|3D ax,iw|81/7 rmw,iw|83/7 rmw,ibx", AddressSize = 16 | 32)]
        public static void WordCompare(Processor p, ushort value1, ushort value2)
        {
            int sResult = (short)value1 - (short)value2;
            uint uResult = (uint)value1 - (uint)value2;

            p.WordUpdateFlags(sResult, uResult, EFlags.Carry | EFlags.Overflow | EFlags.Sign | EFlags.Zero | EFlags.Parity);
        }
        [Alternate("WordCompare", AddressSize = 16 | 32)]
        public static void DWordCompare(Processor p, uint value1, uint value2)
        {
            long sResult = (long)(int)value1 - (long)(int)value2;
            ulong uResult = (ulong)value1 - (ulong)value2;

            p.DWordUpdateFlags(sResult, uResult, EFlags.Carry | EFlags.Overflow | EFlags.Sign | EFlags.Zero | EFlags.Parity);
        }
    }

    internal static class Test
    {
        [Opcode("A8 al,ib|F6/0 rmb,ib|84/r rmb,rb", OperandSize = 16 | 32, AddressSize = 16 | 32)]
        public static void ByteTest(Processor p, byte value1, byte value2)
        {
            byte result = (byte)(value1 & value2);

            p.Flags &= ~(EFlags.Overflow | EFlags.Carry);
            p.ByteUpdateFlags((sbyte)result, result, EFlags.Sign | EFlags.Zero | EFlags.Parity);
        }

        [Opcode("A9 ax,iw|F7/0 rmw,iw|85/r rmw,rw", AddressSize = 16 | 32)]
        public static void WordTest(Processor p, ushort value1, ushort value2)
        {
            ushort result = (ushort)(value1 & value2);

            p.Flags &= ~(EFlags.Overflow | EFlags.Carry);
            p.WordUpdateFlags((short)result, result, EFlags.Sign | EFlags.Zero | EFlags.Parity);
        }
        [Alternate("WordTest", AddressSize = 16 | 32)]
        public static void DWordTest(Processor p, uint value1, uint value2)
        {
            uint result = (uint)(value1 & value2);

            p.Flags &= ~(EFlags.Overflow | EFlags.Carry);
            p.DWordUpdateFlags((int)result, result, EFlags.Sign | EFlags.Zero | EFlags.Parity);
        }
    }
}
